package com.github.pig.training.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.service.SysUserService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.R;
import com.github.pig.training.entity.TrainingStudent;
import com.github.pig.training.entity.TrainingStudentUnRegister;
import com.github.pig.training.mapper.TrainingStudentUnRegisterMapper;
import com.github.pig.training.service.ITrainingStudentService;
import com.github.pig.training.service.ITrainingStudentUnRegisterService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author andrew
 * @since 2018-06-26
 */
@Service
public class TrainingStudentUnRegisterServiceImpl extends ServiceImpl<TrainingStudentUnRegisterMapper, TrainingStudentUnRegister> implements ITrainingStudentUnRegisterService {

    @Autowired
    private ITrainingStudentService studentService;
    @Autowired
    private SysUserService sysUserService;
    @Override
    @Transactional(rollbackFor=Exception.class)
    public R<Boolean> dropOutOfSchool(TrainingStudentUnRegister trainingStudentUnRegister,String name){
        int uid = sysUserService.findUserByUsername(name).getUserId();
        EntityWrapper<TrainingStudent> nw = new EntityWrapper<>();
        nw.where("fid={0}",trainingStudentUnRegister.getStudentId());
        TrainingStudent t = studentService.selectOne(nw);
        if(Integer.valueOf(StringUtils.isEmpty(t.getBizStatus())?"0":t.getBizStatus())!=2208){
            t.setUpdateTime(new Date());
            t.setBizStatus("2208");
            t.setFollowUpUser(0);
            t.setFollowUpStatus("1602");
            studentService.updateById(t);
            trainingStudentUnRegister.setCreateUser(uid);
            trainingStudentUnRegister.setUpdateUser(uid);
            insert(trainingStudentUnRegister);
            return new R<>(Boolean.TRUE);
        } else {
            return new R<>();
        }
    }
}
